package com.hongxun.modules.csg.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.hongxun.common.validator.ValidatorUtils;
import com.hongxun.common.validator.group.AddGroup;
import com.hongxun.common.validator.group.UpdateGroup;
import com.hongxun.modules.csg.entity.CScheduleEntity;
import com.hongxun.modules.csg.service.CScheduleService;
import com.hongxun.modules.sys.entity.SysUserEntity;
import com.hongxun.modules.sys.service.ShiroService;
import com.hongxun.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.csg.entity.CScheduleReportEntity;
import com.hongxun.modules.csg.service.CScheduleReportService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 进度填报信息
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-11 09:33:49
 */
@RestController
@RequestMapping("/csg/cschedulereport")
public class CScheduleReportController {
	@Autowired
	private CScheduleReportService cScheduleReportService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	protected ShiroService shiroService;
	@Autowired
	private CScheduleService cScheduleService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
        if(params.get("sGuid")!= null && !params.get("sGuid").toString().equals("")){
        	wheres += " and s_guid='"+params.get("sGuid").toString()+"'";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")){
        	wheres += " and s_r_datastate != '删除' ";
		}
        query.put("filterSql",wheres+" ");
		List<CScheduleReportEntity> cScheduleReportList = cScheduleReportService.queryList(query);
		int total = cScheduleReportService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(cScheduleReportList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}



	@RequestMapping("/select")
	public R select(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		String wheres =" 1=1 ";
		List<CScheduleReportEntity> cScheduleReportEntities = new ArrayList<>();
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("sGuid")!=null&&!query.get("sGuid").toString().equals(""))
		{
			wheres+=" and  s_guid = '" + query.get("sGuid").toString()+"' ";
		}


		wheres+=" and  s_r_datastate != '删除' ";
		query.put("filterSql",wheres+" ");
		cScheduleReportEntities = cScheduleReportService.queryList(query);
		return R.ok().put("returnlist", cScheduleReportEntities);
	}




	@RequestMapping("/selectToUpdatetime")
	public R selectToUpdatetime(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		Map<String , Object> map = new HashMap<>();
		String wheres =" 1=1 ";
		if(params.get("pGuid")!=null && !params.get("pGuid").toString().equals("")){
			wheres += " and p_guid='"+params.get("pGuid").toString()+"'";
		}
		map.put("filterSql",wheres);
		List<CScheduleReportEntity> cScheList = new ArrayList<>();
		cScheList = cScheduleReportService.queryListToUpdatetime(map);
		return R.ok().put("returnlist", cScheList);
	}




	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		CScheduleReportEntity cScheduleReport = cScheduleReportService.queryObject(guid);
		
		return R.ok().put("cScheduleReport", cScheduleReport);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody CScheduleReportEntity cScheduleReport){
		cScheduleReport.setSRDatastate("新增");
		cScheduleReportService.save(cScheduleReport);
		
		return R.ok();
	}


	/**
	 * 新增进度填报
	 *
	 * @param cScheduleReport
	 * @param sGuid
	 * @param pGuid
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveNew/{sGuid}/{pGuid}")
	public R save(@RequestBody CScheduleReportEntity cScheduleReport , @PathVariable("sGuid") String sGuid ,
				  @PathVariable("pGuid") String pGuid , HttpServletRequest request) throws ParseException {
		ValidatorUtils.validateEntity(cScheduleReport, AddGroup.class);
		//从header中获取token
		String token = request.getHeader("token");
		//如果header中不存在token，则从参数中获取token
		if(StringUtils.isBlank(token)){
			token = request.getParameter("token");
		}
		SysUserEntity sysUserEntity = shiroService.queryByToken(token);

		CScheduleEntity cScheduleEntity = cScheduleService.queryObject(sGuid);
//		CScheduleEntity csch = new CScheduleEntity();

		//保存进度填报信息
		cScheduleReport.setSRUsername(sysUserEntity.getUsername());
		cScheduleReport.setSRUser(sysUserEntity.getUserId());
		cScheduleReport.setSGuid(sGuid);
		cScheduleReport.setSName(cScheduleEntity.getSName());
		cScheduleReport.setPGuid(pGuid);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		df.format(new Date());
		cScheduleReport.setSRUpdatetime(df.parse(df.format(new Date())));
		cScheduleReport.setSRDatastate("新增");

		cScheduleReportService.save(cScheduleReport);

		//将更新时间返回给项目最新进度
		cScheduleEntity.setSTime(cScheduleReport.getSRUpdatetime());
		cScheduleService.update(cScheduleEntity);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody CScheduleReportEntity cScheduleReport){
		ValidatorUtils.validateEntity(cScheduleReport, UpdateGroup.class);
		cScheduleReport.setSRDatastate("修改");
		cScheduleReportService.update(cScheduleReport);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		cScheduleReportService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
